<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>如何爬取知乎live-2022秋 | 杨一赫的博客</title><meta name="author" content="杨一赫"><meta name="copyright" content="杨一赫"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="如何爬取知乎live ​ ——杨一赫 ​ 个人原创，未经允许，不予转载  准备工作   安装好 Pycharm,并成功运行 了解 Python HTTP 请求库 requests 的基本用法。 了解 Ajax 基础知识和分析 Ajax 的基本方法  爬取目标  本次爬取的目标为 知乎 - Live (zhihu.com)   image-2022061315525">
<meta property="og:type" content="article">
<meta property="og:title" content="如何爬取知乎live-2022秋">
<meta property="og:url" content="http://yang1he.gitee.io/2022/07/05/%E5%A6%82%E4%BD%95%E7%88%AC%E5%8F%96%E7%9F%A5%E4%B9%8Elive/index.html">
<meta property="og:site_name" content="杨一赫的博客">
<meta property="og:description" content="如何爬取知乎live ​ ——杨一赫 ​ 个人原创，未经允许，不予转载  准备工作   安装好 Pycharm,并成功运行 了解 Python HTTP 请求库 requests 的基本用法。 了解 Ajax 基础知识和分析 Ajax 的基本方法  爬取目标  本次爬取的目标为 知乎 - Live (zhihu.com)   image-2022061315525">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20220613155253825.png">
<meta property="article:published_time" content="2022-07-04T16:00:00.000Z">
<meta property="article:modified_time" content="2023-02-23T09:02:33.218Z">
<meta property="article:author" content="杨一赫">
<meta property="article:tag" content="-爬虫">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20220613155253825.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://yang1he.gitee.io/2022/07/05/%E5%A6%82%E4%BD%95%E7%88%AC%E5%8F%96%E7%9F%A5%E4%B9%8Elive/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta/><meta/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?6b9e2c1c603a1d11d43a9822bdd68c2c";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: {"limitDay":90,"position":"top","messagePrev":"距离上次更新已经","messageNext":"天本文可能会过期"},
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '如何爬取知乎live-2022秋',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-02-23 17:02:33'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><script src="https://cdn.jsdelivr.net/npm/echarts@5.3.0/dist/echarts.min.js"></script><script src="https://cdn.jsdelivr.net/npm/echarts@4.7.0/map/js/china.min.js"></script><script src="node_modules/simplex-noise/simplex-noise.js"></script><meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/atom.xml" title="杨一赫的博客" type="application/atom+xml">
</head><body><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pace-js/themes/blue/pace-theme-minimal.min.css"/><script src="https://cdn.jsdelivr.net/npm/pace-js/pace.min.js"></script><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/favicon2.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">14</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">7</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">16</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about"><i class="fa-fw fa fa-heart"></i><span> 关于我</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20220613161337621.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">杨一赫的博客</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about"><i class="fa-fw fa fa-heart"></i><span> 关于我</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">如何爬取知乎live-2022秋</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-07-04T16:00:00.000Z" title="发表于 2022-07-05 00:00:00">2022-07-05</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-02-23T09:02:33.218Z" title="更新于 2023-02-23 17:02:33">2023-02-23</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/python/">python</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/python/%E7%88%AC%E8%99%AB/">爬虫</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="如何爬取知乎live-2022秋"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="如何爬取知乎live">如何爬取知乎live</h1>
<p>​ ——杨一赫</p>
<p>​ 个人原创，未经允许，不予转载</p>
<hr />
<h2 id="准备工作">准备工作</h2>
<hr />
<ul>
<li><p>安装好 Pycharm,并成功运行</p></li>
<li><p>了解 Python HTTP 请求库 requests 的基本用法。</p></li>
<li><p>了解 Ajax 基础知识和分析 Ajax 的基本方法</p></li>
</ul>
<h3 id="爬取目标">爬取目标</h3>
<hr />
<p>本次爬取的目标为 <a
target="_blank" rel="noopener" href="https://www.zhihu.com/xen/market/vip/live-lecture">知乎 - Live
(zhihu.com)</a></p>
<figure>
<img
src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20220613155253825.png"
alt="image-20220613155253825" />
<figcaption aria-hidden="true">image-20220613155253825</figcaption>
</figure>
<h3 id="需要完成的任务">需要完成的任务</h3>
<hr />
<p>本节中我们需要完成的<strong>目标</strong>如下。</p>
<ul>
<li><p>分析页面数据的<strong>加载逻辑</strong>。</p></li>
<li><p>用 requests 实现 <strong>Ajax</strong> 数据的爬取。</p></li>
<li><p>将每场live的数据保存到本地文件。</p></li>
</ul>
<h2 id="探索live网页架构">探索live网页架构</h2>
<hr />
<h3 id="查询网页源码">查询网页源码</h3>
<hr />
<p>​ 网页结果就只有<strong>这么一点 HTML
</strong>内容，而我们在浏览器中打开这个页面，却能看到如图所示的<strong>结果</strong></p>
<p><img src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20220613154700461.png" alt="image-20220613154700461" style="zoom:70%;" />
在 HTML 中，我们只能看到在源码中引用了一些 JavaScript 和 CSS
文件，并没有观察到有任何live数据信息</p>
<p>​ 这些数据都是通过 <strong>Ajax </strong>来加载的， JavaScript
在后台调用这些 Ajax
数据接口，得到数据之后，再把数据进行解析并渲染呈现出来，得到最终的页面。所以说，要想爬取这个页面，我们可以直接爬取
Ajax 接口获取数据就好了。</p>
<h3 id="分析ajax接口逻辑">分析AJax接口逻辑</h3>
<hr />
<p>​ 首先我们来分析一下列表页的 Ajax
接口逻辑，打开浏览器开发者工具，切换到 Network 面板，勾选上 Preserve Log
并切换到 XHR 选项卡，接着重新刷新页面，向下滚动，如图所示：</p>
<figure>
<img
src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20220613160349407.png"
alt="image-20220613160349407" />
<figcaption aria-hidden="true">image-20220613160349407</figcaption>
</figure>
<p>​ 我们可以点击查看其请求详情。观察其请求的 URL
和参数以及响应内容是怎样的，如图所示：</p>
<figure>
<img
src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20220613160448694.png"
alt="image-20220613160448694" />
<figcaption aria-hidden="true">image-20220613160448694</figcaption>
</figure>
<p>​ 将多个<code>Request URL</code>放在一起：</p>
<figure class="highlight http"><table><tr><td class="code"><pre><span class="line">https://api.zhihu.com/market/categories/alllimit=10&amp;dataType=new&amp;study_type=Live&amp;sort_type=hottest&amp;offset=20&amp;level=2&amp;right_type=</span><br></pre></td></tr></table></figure>
<figure class="highlight http"><table><tr><td class="code"><pre><span class="line">https://api.zhihu.com/market/categories/alllimit=10&amp;dataType=new&amp;study_type=Live&amp;sort_type=hottest&amp;offset=30&amp;level=2&amp;right_type=</span><br></pre></td></tr></table></figure>
<p>​ 我们可以观察到这么一个规律：<code>limit</code> 一直为
10，这就正好对应着每页 10 条数据；<code>offset</code>
在依次变大，页面每加 1 页，<code>offset</code> 就加
10，这就代表着页面的数据偏移量</p>
<p>​ 接着我们再观察一下响应的数据，切换到 Preview
选项卡，结果如图所示：</p>
<figure>
<img
src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20220613161337621.png"
alt="image-20220613161337621" />
<figcaption aria-hidden="true">image-20220613161337621</figcaption>
</figure>
<p>​ 可以看到，结果就是一些 JSON 数据，它有一个 <code>data</code>
字段，是一个列表，列表中每一个元素都是一个字典。观察一下字典的内容，这里我们正好可以看到有对应的电影数据的字段了，如
<code>title、</code>discription<code>、</code>online time
text`，对比下浏览器中的真实数据，各个内容完全一致，而且这个数据已经非常结构化了。</p>
<h2 id="编写爬虫">编写爬虫</h2>
<hr />
<h3 id="定义一些准备工作">定义一些准备工作</h3>
<hr />
<p>​ 导入一些所需的库并定义一些配置，代码如下：</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">from</span> __future__ <span class="keyword">import</span> unicode_literals<span class="comment">#前三个是</span></span><br><span class="line"><span class="keyword">from</span> __future__ <span class="keyword">import</span> print_function</span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line"><span class="keyword">from</span> gooey <span class="keyword">import</span> Gooey, GooeyParser</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> time</span><br></pre></td></tr></table></figure>
<p>​
为了构造<code>Request URL</code>,我们需要输入一些参数<code>User-Agent</code>,<code>Cookie</code>,
爬取的数据需求量<code>page</code>以及临时存放数据用的列表
<code>list</code>。</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">zhihulive</span>():</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self,ur:<span class="built_in">str</span>,User_Agent:<span class="built_in">str</span>,Cookie:<span class="built_in">str</span>,page:<span class="built_in">int</span></span>):</span><br><span class="line">        self.ur=ur</span><br><span class="line">        self.headers=&#123;<span class="string">&#x27;User-Agent&#x27;</span>: User_Agent,</span><br><span class="line">                   <span class="string">&#x27;Cookie&#x27;</span>:Cookie</span><br><span class="line">                   &#125;</span><br><span class="line">        self.page=page</span><br><span class="line">        self.li = <span class="built_in">list</span>()</span><br></pre></td></tr></table></figure>
<h3 id="构造循环批量爬取">构造循环批量爬取</h3>
<hr />
<p>​ 这里我们定义了一个 <code>run</code> 方法，这个方法专门用来处理 JSON
接口，接着 <code>response</code> 调用的是 <code>json</code>
方法，它可以解析响应的内容并将其转化成 JSON 字符串。</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">run</span>(<span class="params">self</span>):</span><br><span class="line">    <span class="built_in">print</span>()</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">                           .__    .__.__                    .__  .__              </span></span><br><span class="line"><span class="string">            _______|  |__ |__|  |__  __ __          |  | |__|__  __ ____  </span></span><br><span class="line"><span class="string">            \___   /  |  \|  |  |  \|  |  \  ______ |  | |  \  \/ // __ \ </span></span><br><span class="line"><span class="string">             /    /|   Y  \  |   Y  \  |  / /_____/ |  |_|  |\   /\  ___/ </span></span><br><span class="line"><span class="string">            /_____ \___|  /__|___|  /____/          |____/__| \_/  \___  &gt;</span></span><br><span class="line"><span class="string">                  \/    \/        \/                                   \/ </span></span><br><span class="line"><span class="string">            &quot;&quot;&quot;</span>)</span><br><span class="line">    starttime = time.time()</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(self.page):</span><br><span class="line">        offset = i * <span class="number">10</span></span><br><span class="line">        INDEX_URL = self.ur.<span class="built_in">format</span>(offset=offset)</span><br><span class="line">        response = requests.get(url=INDEX_URL, headers=self.headers)</span><br><span class="line">        x = response.json()</span><br><span class="line">        <span class="keyword">for</span> j <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>):</span><br><span class="line">            <span class="built_in">id</span> = x.get(<span class="string">&#x27;data&#x27;</span>)[j].get(<span class="string">&#x27;business_id&#x27;</span>)</span><br><span class="line">            DETAIL = <span class="string">&#x27;https://api.zhihu.com/lives/&#123;id&#125;&#x27;</span></span><br><span class="line">            other = <span class="string">&#x27;https://api.zhihu.com/details/recommend/live/&#123;id&#125;&#x27;</span></span><br><span class="line">            DETAIL_URL, other_url = DETAIL.<span class="built_in">format</span>(<span class="built_in">id</span>=<span class="built_in">id</span>), other.<span class="built_in">format</span>(<span class="built_in">id</span>=<span class="built_in">id</span>)</span><br><span class="line">			res_detail=requests.get(url=DETAIL_URL,headers=self.headers)</span><br><span class="line">			res_other=requests.get(url=other_url,headers=self.headers)</span><br><span class="line">            y, z = res_detail.json(), res_other.json()</span><br><span class="line">            out = <span class="built_in">dict</span>(y, **z)</span><br><span class="line">            self.li.append(out)</span><br><span class="line">            progress=<span class="built_in">int</span>((i*<span class="number">10</span>+j+<span class="number">1</span>)/(self.page*<span class="number">10</span>)*<span class="number">100</span>)</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;progress: &#123;&#125;%&quot;</span>.<span class="built_in">format</span>(progress))</span><br><span class="line">            sys.stdout.flush()</span><br><span class="line">                <span class="string">&quot;&quot;&quot;这行代码主要是避免输出到终端的内容被python缓存,但是通过环境变量,-u启动python还有运行的系统,都能影响是否缓存,这段代码能保证在所有的地方都能够每秒输出1个数字,去掉之后在有的地方会出现被缓存的情况.而你的电脑刚好没有使用缓存.好像是windows的很少(不会?)出现被缓存的情况.&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>
<h3 id="使用pandas导出excel">使用pandas导出Excel</h3>
<hr />
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df = pd.DataFrame(self.li)</span><br><span class="line">df.to_excel(<span class="string">&quot;output.xlsx&quot;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">endtime = time.time()</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;总共的时间为:&#x27;</span>, <span class="built_in">round</span>(endtime - starttime, <span class="number">2</span>), <span class="string">&#x27;secs&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;完美运行，结果在output.xlsx里，快去查看吧&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h3 id="输入你的信息并运行吧">输入你的信息并运行吧</h3>
<hr />
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">ur= <span class="string">&quot;http://api.zhihu.com/market/categories/all?study_type=Live&amp;sort_type=hottest&amp;limit=10&amp;offset=&#123;offset&#125;&amp;dataType=new&amp;level=2&amp;right_type=&quot;</span></span><br><span class="line">User_Agent=<span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36&#x27;</span></span><br><span class="line">Cookie=<span class="string">&#x27;_zap=8740f509-595a-41bb-b02a-de23ca8fc7a0; _xsrf=Jso6aDVxIJl0B1axjw3S5p9uTIRBsjos; d_c0=&quot;APCcqv9aEhWPTlpQUm7m0hGPD5xRONWRrNo=|1654789067&quot;; l_cap_id=&quot;NTZhMzg5MmFmNjc5NDAwNWFlYjdjYTg0ZDRhNjA2YTk=|1654789073|c8582cf3c15d1136d33c18284eba876d295ea553&quot;; r_cap_id=&quot;YmViZjM1Zjg2MTcxNDc3ZDljNmI1MTU2NjQ3NTAxNWI=|1654789073|0cffb4e767235912bdaa609160a895c5e457cd5b&quot;; cap_id=&quot;ZDI2ZDk1YjFiZTRmNGM4OWE0NWQ4ODE1NjNlOTJhM2E=|1654789073|58236864fad7f0107a9ca88b21d420a5f4af6e52&quot;; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1654789068,1654834996,1654849617; tst=h; NOT_UNREGISTER_WAITING=1; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1654863768; captcha_session_v2=2|1:0|10:1654863767|18:captcha_session_v2|88:SVMyeFhZdDVNc1k1M0I3QTI0NnkwcjlLQnJhOTFnd1FhMnFDdXBySTJXb3E1Zm1LWSthUTR5WldMcTcwZHZLYw==|a02d1f6108d7a87fdc4056156345ae0f9e737ac5183deb91d4245ce0e312568b; KLBRSID=f48cb29c5180c5b0d91ded2e70103232|1654863813|1654859801&#x27;</span></span><br><span class="line">t=zhihulive(ur,User_Agent,Cookie,<span class="number">10</span>)</span><br><span class="line">t.run()</span><br></pre></td></tr></table></figure>
<h2 id="最后但没有结束">最后但没有结束</h2>
<hr />
<h3 id="用gooey开发一个gui界面">用gooey开发一个gui界面</h3>
<hr />
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">from</span> gooey <span class="keyword">import</span> Gooey, GooeyParser</span><br><span class="line"></span><br><span class="line"><span class="meta">@Gooey(<span class="params">language=<span class="string">&#x27;Chinese&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>,program_name=<span class="string">&quot;工具&quot;</span>,</span></span></span><br><span class="line"><span class="params"><span class="meta">       progress_regex=<span class="string">r&quot;^progress: (\d+)%$&quot;</span>,disable_stop_button=<span class="literal">True</span></span>)</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">main</span>():</span><br><span class="line">    <span class="string">&quot;&quot;&quot;传入&quot;&quot;&quot;</span></span><br><span class="line">    parser = GooeyParser(description=<span class="string">&quot;知乎live爬取器&quot;</span>)</span><br><span class="line">    parser.add_argument(<span class="string">&#x27;ur&#x27;</span>,</span><br><span class="line">                         <span class="built_in">help</span>=<span class="string">&quot;所要爬取的url&quot;</span>,</span><br><span class="line">                        default=<span class="string">&quot;http://api.zhihu.com/market/categories/all?study_type=Live&amp;sort_type=hottest&amp;limit=10&amp;offset=&#123;offset&#125;&amp;dataType=new&amp;level=2&amp;right_type=&quot;</span></span><br><span class="line">                           )</span><br><span class="line">    parser.add_argument(<span class="string">&#x27;User_Agent&#x27;</span>,</span><br><span class="line">                        <span class="built_in">help</span>=<span class="string">&quot;自己的的User_Agent&quot;</span>,</span><br><span class="line">                        default=<span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36&#x27;</span></span><br><span class="line">                        )</span><br><span class="line">    parser.add_argument(<span class="string">&#x27;Cookie&#x27;</span>,</span><br><span class="line">                        <span class="built_in">help</span>=<span class="string">&quot;自己的的Cookie&quot;</span>,</span><br><span class="line">                        default=<span class="string">&#x27;_zap=8740f509-595a-41bb-b02a-de23ca8fc7a0; _xsrf=Jso6aDVxIJl0B1axjw3S5p9uTIRBsjos; d_c0=&quot;APCcqv9aEhWPTlpQUm7m0hGPD5xRONWRrNo=|1654789067&quot;; l_cap_id=&quot;NTZhMzg5MmFmNjc5NDAwNWFlYjdjYTg0ZDRhNjA2YTk=|1654789073|c8582cf3c15d1136d33c18284eba876d295ea553&quot;; r_cap_id=&quot;YmViZjM1Zjg2MTcxNDc3ZDljNmI1MTU2NjQ3NTAxNWI=|1654789073|0cffb4e767235912bdaa609160a895c5e457cd5b&quot;; cap_id=&quot;ZDI2ZDk1YjFiZTRmNGM4OWE0NWQ4ODE1NjNlOTJhM2E=|1654789073|58236864fad7f0107a9ca88b21d420a5f4af6e52&quot;; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1654789068,1654834996,1654849617; tst=h; NOT_UNREGISTER_WAITING=1; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1654863768; captcha_session_v2=2|1:0|10:1654863767|18:captcha_session_v2|88:SVMyeFhZdDVNc1k1M0I3QTI0NnkwcjlLQnJhOTFnd1FhMnFDdXBySTJXb3E1Zm1LWSthUTR5WldMcTcwZHZLYw==|a02d1f6108d7a87fdc4056156345ae0f9e737ac5183deb91d4245ce0e312568b; KLBRSID=f48cb29c5180c5b0d91ded2e70103232|1654863813|1654859801&#x27;</span></span><br><span class="line">                        )</span><br><span class="line">    <span class="comment"># parser.add_argument(&quot;-p&quot;, &quot;--pause&quot;, action=&quot;store_true&quot;, help=&quot;Pause execution&quot;)#可选参数框,为以后添加功能</span></span><br><span class="line">    parser.add_argument(<span class="string">&#x27;num&#x27;</span>, <span class="built_in">help</span>=<span class="string">&quot;所要爬取元素个数，1page等于10个live&quot;</span>, default=<span class="number">1</span>)</span><br><span class="line">    args = parser.parse_args()</span><br><span class="line">    <span class="string">&quot;&quot;&quot;传出&quot;&quot;&quot;</span></span><br><span class="line">    t=zhihulive(<span class="built_in">str</span>(args.ur),<span class="built_in">str</span>(args.User_Agent),<span class="built_in">str</span>(args.Cookie),<span class="built_in">int</span>(args.num))</span><br><span class="line">    t.run()</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    main()</span><br></pre></td></tr></table></figure>
<p>至此，爬取项目全部完成</p>
<h3 id="参考文献">参考文献</h3>
<p>什么是 Ajax？ ：https://cuiqingcai.com/202251.html</p>
<p>gooey官方文档：<a
target="_blank" rel="noopener" href="https://github.com/chriskiehl/GooeyExamples/tree/master/examples">粘伊示例/示例在大师
·克里斯凯尔/古伊示例 ·GitHub</a></p>
<h3 id="开发历史过程">开发历史过程</h3>
<hr />
<p>​ 以下是开发历史过程，仅作为记录，不保证能运行，也不算入项目代码</p>
<p>​ <strong>未加循环单次爬取测试</strong></p>
<hr />
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#未加循环单次爬取测试</span></span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> logging</span><br><span class="line">INDEX_URL= <span class="string">&quot;http://api.zhihu.com/market/categories/all?study_type=Live&amp;sort_type=hottest&amp;limit=10&amp;offset=&#123;offset&#125;&amp;dataType=new&amp;level=2&amp;right_type=&quot;</span></span><br><span class="line"></span><br><span class="line">headers = &#123;<span class="string">&#x27;User-Agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36&#x27;</span>,</span><br><span class="line">               <span class="string">&#x27;Cookie&#x27;</span>:<span class="string">&#x27;_zap=8740f509-595a-41bb-b02a-de23ca8fc7a0; _xsrf=Jso6aDVxIJl0B1axjw3S5p9uTIRBsjos; d_c0=&quot;APCcqv9aEhWPTlpQUm7m0hGPD5xRONWRrNo=|1654789067&quot;; l_cap_id=&quot;NTZhMzg5MmFmNjc5NDAwNWFlYjdjYTg0ZDRhNjA2YTk=|1654789073|c8582cf3c15d1136d33c18284eba876d295ea553&quot;; r_cap_id=&quot;YmViZjM1Zjg2MTcxNDc3ZDljNmI1MTU2NjQ3NTAxNWI=|1654789073|0cffb4e767235912bdaa609160a895c5e457cd5b&quot;; cap_id=&quot;ZDI2ZDk1YjFiZTRmNGM4OWE0NWQ4ODE1NjNlOTJhM2E=|1654789073|58236864fad7f0107a9ca88b21d420a5f4af6e52&quot;; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1654789068,1654834996,1654849617; tst=h; NOT_UNREGISTER_WAITING=1; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1654863768; captcha_session_v2=2|1:0|10:1654863767|18:captcha_session_v2|88:SVMyeFhZdDVNc1k1M0I3QTI0NnkwcjlLQnJhOTFnd1FhMnFDdXBySTJXb3E1Zm1LWSthUTR5WldMcTcwZHZLYw==|a02d1f6108d7a87fdc4056156345ae0f9e737ac5183deb91d4245ce0e312568b; KLBRSID=f48cb29c5180c5b0d91ded2e70103232|1654863813|1654859801&#x27;</span>,</span><br><span class="line">              &#125;</span><br><span class="line">response = requests.get(url=INDEX_URL, headers=headers)</span><br><span class="line">x=response.json()</span><br><span class="line"><span class="built_in">id</span> = x.get(<span class="string">&#x27;data&#x27;</span>)[<span class="number">1</span>].get(<span class="string">&#x27;business_id&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">id</span>)</span><br><span class="line">DETAIL = <span class="string">&#x27;https://api.zhihu.com/details/recommend/live/&#123;id&#125;&#x27;</span></span><br><span class="line">DETAIL_URL=DETAIL.<span class="built_in">format</span>(<span class="built_in">id</span>=<span class="built_in">id</span>)</span><br><span class="line">res=requests.get(url=DETAIL, headers=headers)</span><br><span class="line">y=response.json()</span><br><span class="line"><span class="built_in">print</span>(y)</span><br></pre></td></tr></table></figure>
<p><strong>加循环批量爬取</strong>测试</p>
<hr />
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#加循环批量爬取</span></span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line">ur= <span class="string">&quot;http://api.zhihu.com/market/categories/all?study_type=Live&amp;sort_type=hottest&amp;limit=10&amp;offset=&#123;offset&#125;&amp;dataType=new&amp;level=2&amp;right_type=&quot;</span></span><br><span class="line">headers = &#123;<span class="string">&#x27;User-Agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36&#x27;</span>,</span><br><span class="line">               <span class="string">&#x27;Cookie&#x27;</span>:<span class="string">&#x27;_zap=8740f509-595a-41bb-b02a-de23ca8fc7a0; _xsrf=Jso6aDVxIJl0B1axjw3S5p9uTIRBsjos; d_c0=&quot;APCcqv9aEhWPTlpQUm7m0hGPD5xRONWRrNo=|1654789067&quot;; l_cap_id=&quot;NTZhMzg5MmFmNjc5NDAwNWFlYjdjYTg0ZDRhNjA2YTk=|1654789073|c8582cf3c15d1136d33c18284eba876d295ea553&quot;; r_cap_id=&quot;YmViZjM1Zjg2MTcxNDc3ZDljNmI1MTU2NjQ3NTAxNWI=|1654789073|0cffb4e767235912bdaa609160a895c5e457cd5b&quot;; cap_id=&quot;ZDI2ZDk1YjFiZTRmNGM4OWE0NWQ4ODE1NjNlOTJhM2E=|1654789073|58236864fad7f0107a9ca88b21d420a5f4af6e52&quot;; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1654789068,1654834996,1654849617; tst=h; NOT_UNREGISTER_WAITING=1; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1654863768; captcha_session_v2=2|1:0|10:1654863767|18:captcha_session_v2|88:SVMyeFhZdDVNc1k1M0I3QTI0NnkwcjlLQnJhOTFnd1FhMnFDdXBySTJXb3E1Zm1LWSthUTR5WldMcTcwZHZLYw==|a02d1f6108d7a87fdc4056156345ae0f9e737ac5183deb91d4245ce0e312568b; KLBRSID=f48cb29c5180c5b0d91ded2e70103232|1654863813|1654859801&#x27;</span>,</span><br><span class="line">              &#125;</span><br><span class="line">li=<span class="built_in">list</span>()</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>):</span><br><span class="line">    offset=i*<span class="number">10</span></span><br><span class="line">    INDEX_URL=ur.<span class="built_in">format</span>(offset=offset)</span><br><span class="line">    response = requests.get(url=INDEX_URL, headers=headers)</span><br><span class="line">    x = response.json()</span><br><span class="line">    <span class="keyword">for</span> j <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>):</span><br><span class="line">        <span class="built_in">id</span> = x.get(<span class="string">&#x27;data&#x27;</span>)[j].get(<span class="string">&#x27;business_id&#x27;</span>)</span><br><span class="line">        DETAIL = <span class="string">&#x27;https://api.zhihu.com/lives/&#123;id&#125;&#x27;</span></span><br><span class="line">        other=<span class="string">&#x27;https://api.zhihu.com/details/recommend/live/&#123;id&#125;&#x27;</span></span><br><span class="line">        DETAIL_URL,other_url = DETAIL.<span class="built_in">format</span>(<span class="built_in">id</span>=<span class="built_in">id</span>),other.<span class="built_in">format</span>(<span class="built_in">id</span>=<span class="built_in">id</span>)</span><br><span class="line">        res_detail,res_other  = requests.get(url=DETAIL_URL,headers=headers),requests.get(url=other_url,headers=headers)</span><br><span class="line">        y,z= res_detail.json(),res_other.json()</span><br><span class="line">        out=<span class="built_in">dict</span>(y,**z)</span><br><span class="line">        li.append(out)</span><br><span class="line">df=pd.DataFrame(li)</span><br><span class="line">df.to_excel(<span class="string">&quot;output.xlsx&quot;</span>)</span><br></pre></td></tr></table></figure>
<h3 id="封装exe文件">封装exe文件</h3>
<p>​ 安装pyinstaller,在pycharm终端输入</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">pyinstaller -F -w test3.py</span><br></pre></td></tr></table></figure>
<p>​ 等待1分钟，打包完成</p>
<p><a
target="_blank" rel="noopener" href="https://www.jianshu.com/p/4eaad2163567">PowerShell：因为在此系统上禁止运行脚本，解决方法
- 简书 (jianshu.com)</a></p>
<p><a
target="_blank" rel="noopener" href="https://blog.csdn.net/hdudb/article/details/122055537">(8条消息)
Pyinstaller
详解多种打包过程(去坑,填坑)。_垂落的博客-CSDN博客_pyinstaller打包</a></p>
<h3 id="连接mongodb数据库">连接mongodb数据库</h3>
<p><a
target="_blank" rel="noopener" href="https://blog.csdn.net/nanhuaibeian/article/details/108183715?ops_request_misc=%7B%22request%5Fid%22%3A%22165518883916782425113876%22%2C%22scm%22%3A%2220140713.130102334..%22%7D&amp;request_id=165518883916782425113876&amp;biz_id=0&amp;utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-108183715-null-null.142%5Ev14%5Epc_search_result_control_group,157%5Ev14%5Enew_3&amp;utm_term=MongoDB&amp;spm=1018.2226.3001.4187">(8条消息)
学习 MongoDB
一篇文章就够了（珍藏版）_南淮北安的博客-CSDN博客_mongodb文章</a></p>
<p><a target="_blank" rel="noopener" href="https://www.runoob.com/mongodb/nosql.html">NoSQL 简介 |
菜鸟教程 (runoob.com)</a></p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> sys</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="comment"># import pandas as pd #没用到pandas处理</span></span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> pymongo <span class="comment">#pycharm配置mongodb数据库</span></span><br><span class="line"><span class="keyword">from</span> gooey <span class="keyword">import</span> Gooey, GooeyParser</span><br><span class="line"><span class="comment">#构建存储数据库</span></span><br><span class="line">verison=time.time()</span><br><span class="line">myclient = pymongo.MongoClient(<span class="string">&quot;mongodb://localhost:27017&quot;</span>)  <span class="comment"># Host以及port</span></span><br><span class="line">db = myclient[<span class="string">&quot;test_zhihu_data&quot;</span>]  <span class="comment">#创建数据库</span></span><br><span class="line">coll = db[<span class="string">&quot;zhihu_live&#123;&#125;&quot;</span>.<span class="built_in">format</span>(verison)] <span class="comment"># 这样每一次爬取都能单独存储</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">zhihulive</span>():</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self,ur:<span class="built_in">str</span>,User_Agent:<span class="built_in">str</span>,Cookie:<span class="built_in">str</span>,page</span>):</span><br><span class="line">        self.ur=ur</span><br><span class="line">        self.headers=&#123;<span class="string">&#x27;User-Agent&#x27;</span>: User_Agent,</span><br><span class="line">                   <span class="string">&#x27;Cookie&#x27;</span>:Cookie</span><br><span class="line">                   &#125;</span><br><span class="line">        self.page=page</span><br><span class="line">        self.li = <span class="built_in">list</span>()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">run</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">                               .__    .__.__                    .__  .__              </span></span><br><span class="line"><span class="string">                _______|  |__ |__|  |__  __ __          |  | |__|__  __ ____  </span></span><br><span class="line"><span class="string">                \___   /  |  \|  |  |  \|  |  \  ______ |  | |  \  \/ // __ \ </span></span><br><span class="line"><span class="string">                 /    /|   Y  \  |   Y  \  |  / /_____/ |  |_|  |\   /\  ___/ </span></span><br><span class="line"><span class="string">                /_____ \___|  /__|___|  /____/          |____/__| \_/  \___  &gt;</span></span><br><span class="line"><span class="string">                      \/    \/        \/                                   \/ </span></span><br><span class="line"><span class="string">                &quot;&quot;&quot;</span>)</span><br><span class="line">        starttime = time.time()</span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(self.page):</span><br><span class="line">            offset = i * <span class="number">10</span></span><br><span class="line">            INDEX_URL = self.ur.<span class="built_in">format</span>(offset=offset)</span><br><span class="line">            response = requests.get(url=INDEX_URL, headers=self.headers)</span><br><span class="line">            x = response.json()</span><br><span class="line">            <span class="keyword">for</span> j <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>):</span><br><span class="line">                <span class="built_in">id</span> = x.get(<span class="string">&#x27;data&#x27;</span>)[j].get(<span class="string">&#x27;business_id&#x27;</span>)</span><br><span class="line">                DETAIL = <span class="string">&#x27;https://api.zhihu.com/lives/&#123;id&#125;&#x27;</span></span><br><span class="line">                other = <span class="string">&#x27;https://api.zhihu.com/details/recommend/live/&#123;id&#125;&#x27;</span></span><br><span class="line">                DETAIL_URL, other_url = DETAIL.<span class="built_in">format</span>(<span class="built_in">id</span>=<span class="built_in">id</span>), other.<span class="built_in">format</span>(<span class="built_in">id</span>=<span class="built_in">id</span>)</span><br><span class="line">                res_detail, res_other = requests.get(url=DETAIL_URL, headers=self.headers),requests.get(url=other_url,headers=self.headers)</span><br><span class="line">                y, z = res_detail.json(), res_other.json()</span><br><span class="line">                out = <span class="built_in">dict</span>(y, **z)</span><br><span class="line">                coll.insert_one(out)</span><br><span class="line">                progress=<span class="built_in">int</span>((i*<span class="number">10</span>+j+<span class="number">1</span>)/(self.page*<span class="number">10</span>)*<span class="number">100</span>)</span><br><span class="line">                <span class="comment"># print(&quot;\r完成进度&#123;0&#125;%&quot;.format(progress),end=&quot;&quot;,flush=True)</span></span><br><span class="line">                <span class="built_in">print</span>(<span class="string">&quot;progress: &#123;&#125;%&quot;</span>.<span class="built_in">format</span>(progress))</span><br><span class="line">                sys.stdout.flush()</span><br><span class="line">                <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">                这行代码主要是避免输出到终端的内容被python缓存,但是通过环境变量,-u启动python还有运行的系统,都能影响是否缓存,这段代码能保证在所有的地方都能够每秒输出1个数字,去掉之后在有的地方会出现被缓存的情况.</span></span><br><span class="line"><span class="string">                而你的电脑刚好没有使用缓存.</span></span><br><span class="line"><span class="string">                好像是windows的很少(不会?)出现被缓存的情况.&quot;&quot;&quot;</span></span><br><span class="line">        endtime = time.time()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;总共的时间为:&#x27;</span>, <span class="built_in">round</span>(endtime - starttime, <span class="number">2</span>), <span class="string">&#x27;secs&#x27;</span>)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;完美运行，结果在里，快去查看吧&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#基于交互界面爬取</span></span><br><span class="line"></span><br><span class="line"><span class="meta">@Gooey(<span class="params">language=<span class="string">&#x27;Chinese&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>,program_name=<span class="string">&quot;工具&quot;</span>,</span></span></span><br><span class="line"><span class="params"><span class="meta">       progress_regex=<span class="string">r&quot;^progress: (\d+)%$&quot;</span>,disable_stop_button=<span class="literal">True</span></span>)</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">main</span>():</span><br><span class="line">    <span class="string">&quot;&quot;&quot;传入&quot;&quot;&quot;</span></span><br><span class="line">    parser = GooeyParser(description=<span class="string">&quot;知乎live爬取器&quot;</span>)</span><br><span class="line">    parser.add_argument(<span class="string">&#x27;ur&#x27;</span>,</span><br><span class="line">                         <span class="built_in">help</span>=<span class="string">&quot;所要爬取的url&quot;</span>,</span><br><span class="line">                        default=<span class="string">&quot;http://api.zhihu.com/market/categories/all?study_type=Live&amp;sort_type=hottest&amp;limit=10&amp;offset=&#123;offset&#125;&amp;dataType=new&amp;level=2&amp;right_type=&quot;</span></span><br><span class="line">                           )</span><br><span class="line">    parser.add_argument(<span class="string">&#x27;User_Agent&#x27;</span>,</span><br><span class="line">                        <span class="built_in">help</span>=<span class="string">&quot;自己的的User_Agent&quot;</span>,</span><br><span class="line">                        default=<span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36&#x27;</span></span><br><span class="line">                        )</span><br><span class="line">    parser.add_argument(<span class="string">&#x27;Cookie&#x27;</span>,</span><br><span class="line">                        <span class="built_in">help</span>=<span class="string">&quot;自己的的Cookie&quot;</span>,</span><br><span class="line">                        default=<span class="string">&#x27;_zap=8740f509-595a-41bb-b02a-de23ca8fc7a0; _xsrf=Jso6aDVxIJl0B1axjw3S5p9uTIRBsjos; d_c0=&quot;APCcqv9aEhWPTlpQUm7m0hGPD5xRONWRrNo=|1654789067&quot;; l_cap_id=&quot;NTZhMzg5MmFmNjc5NDAwNWFlYjdjYTg0ZDRhNjA2YTk=|1654789073|c8582cf3c15d1136d33c18284eba876d295ea553&quot;; r_cap_id=&quot;YmViZjM1Zjg2MTcxNDc3ZDljNmI1MTU2NjQ3NTAxNWI=|1654789073|0cffb4e767235912bdaa609160a895c5e457cd5b&quot;; cap_id=&quot;ZDI2ZDk1YjFiZTRmNGM4OWE0NWQ4ODE1NjNlOTJhM2E=|1654789073|58236864fad7f0107a9ca88b21d420a5f4af6e52&quot;; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1654789068,1654834996,1654849617; tst=h; NOT_UNREGISTER_WAITING=1; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1654863768; captcha_session_v2=2|1:0|10:1654863767|18:captcha_session_v2|88:SVMyeFhZdDVNc1k1M0I3QTI0NnkwcjlLQnJhOTFnd1FhMnFDdXBySTJXb3E1Zm1LWSthUTR5WldMcTcwZHZLYw==|a02d1f6108d7a87fdc4056156345ae0f9e737ac5183deb91d4245ce0e312568b; KLBRSID=f48cb29c5180c5b0d91ded2e70103232|1654863813|1654859801&#x27;</span></span><br><span class="line">                        )</span><br><span class="line">    <span class="comment"># parser.add_argument(&quot;-p&quot;, &quot;--pause&quot;, action=&quot;store_true&quot;, help=&quot;Pause execution&quot;)#可选参数框</span></span><br><span class="line">    parser.add_argument(<span class="string">&#x27;num&#x27;</span>, <span class="built_in">help</span>=<span class="string">&quot;所要爬取元素个数，1page等于10个live&quot;</span>, default=<span class="number">1</span>)</span><br><span class="line">    args = parser.parse_args()</span><br><span class="line">    <span class="string">&quot;&quot;&quot;传出&quot;&quot;&quot;</span></span><br><span class="line">    t=zhihulive(<span class="built_in">str</span>(args.ur),<span class="built_in">str</span>(args.User_Agent),<span class="built_in">str</span>(args.Cookie),<span class="built_in">int</span>(args.num))</span><br><span class="line">    t.run()</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    main()	</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://yang1he.gitee.io">杨一赫</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://yang1he.gitee.io/2022/07/05/%E5%A6%82%E4%BD%95%E7%88%AC%E5%8F%96%E7%9F%A5%E4%B9%8Elive/">http://yang1he.gitee.io/2022/07/05/%E5%A6%82%E4%BD%95%E7%88%AC%E5%8F%96%E7%9F%A5%E4%B9%8Elive/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://yang1he.gitee.io" target="_blank">杨一赫的博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E7%88%AC%E8%99%AB/">-爬虫</a></div><div class="post_share"><div class="social-share" data-image="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20220613155253825.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/09/15/A%20Multi-Layer%20Fusion%20Neural%20Network/"><img class="prev-cover" src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20220912135851165.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">A Multi-Layer Fusion Neural Network</div></div></a></div><div class="next-post pull-right"><a href="/2021/09/05/pandas%E5%85%A5%E9%97%A8/"><img class="next-cover" src="https://img.freepik.com/premium-psd/plastic-individual-tablecloth-mockup-top-view_1332-10763.jpg?w=1480" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">pandas入门</div></div></a></div></nav><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="gitalk-container"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/favicon2.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">杨一赫</div><div class="author-info__description">阳光开朗大男孩</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">14</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">7</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">16</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/yang1he"><i class="fab fa-github"></i><span>gitee</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">平平无奇的网站</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%A6%82%E4%BD%95%E7%88%AC%E5%8F%96%E7%9F%A5%E4%B9%8Elive"><span class="toc-number">1.</span> <span class="toc-text">如何爬取知乎live</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C"><span class="toc-number">1.1.</span> <span class="toc-text">准备工作</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%88%AC%E5%8F%96%E7%9B%AE%E6%A0%87"><span class="toc-number">1.1.1.</span> <span class="toc-text">爬取目标</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9C%80%E8%A6%81%E5%AE%8C%E6%88%90%E7%9A%84%E4%BB%BB%E5%8A%A1"><span class="toc-number">1.1.2.</span> <span class="toc-text">需要完成的任务</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8E%A2%E7%B4%A2live%E7%BD%91%E9%A1%B5%E6%9E%B6%E6%9E%84"><span class="toc-number">1.2.</span> <span class="toc-text">探索live网页架构</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9F%A5%E8%AF%A2%E7%BD%91%E9%A1%B5%E6%BA%90%E7%A0%81"><span class="toc-number">1.2.1.</span> <span class="toc-text">查询网页源码</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E6%9E%90ajax%E6%8E%A5%E5%8F%A3%E9%80%BB%E8%BE%91"><span class="toc-number">1.2.2.</span> <span class="toc-text">分析AJax接口逻辑</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BC%96%E5%86%99%E7%88%AC%E8%99%AB"><span class="toc-number">1.3.</span> <span class="toc-text">编写爬虫</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89%E4%B8%80%E4%BA%9B%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C"><span class="toc-number">1.3.1.</span> <span class="toc-text">定义一些准备工作</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9E%84%E9%80%A0%E5%BE%AA%E7%8E%AF%E6%89%B9%E9%87%8F%E7%88%AC%E5%8F%96"><span class="toc-number">1.3.2.</span> <span class="toc-text">构造循环批量爬取</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8pandas%E5%AF%BC%E5%87%BAexcel"><span class="toc-number">1.3.3.</span> <span class="toc-text">使用pandas导出Excel</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BE%93%E5%85%A5%E4%BD%A0%E7%9A%84%E4%BF%A1%E6%81%AF%E5%B9%B6%E8%BF%90%E8%A1%8C%E5%90%A7"><span class="toc-number">1.3.4.</span> <span class="toc-text">输入你的信息并运行吧</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%80%E5%90%8E%E4%BD%86%E6%B2%A1%E6%9C%89%E7%BB%93%E6%9D%9F"><span class="toc-number">1.4.</span> <span class="toc-text">最后但没有结束</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%94%A8gooey%E5%BC%80%E5%8F%91%E4%B8%80%E4%B8%AAgui%E7%95%8C%E9%9D%A2"><span class="toc-number">1.4.1.</span> <span class="toc-text">用gooey开发一个gui界面</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E6%96%87%E7%8C%AE"><span class="toc-number">1.4.2.</span> <span class="toc-text">参考文献</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%80%E5%8F%91%E5%8E%86%E5%8F%B2%E8%BF%87%E7%A8%8B"><span class="toc-number">1.4.3.</span> <span class="toc-text">开发历史过程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%81%E8%A3%85exe%E6%96%87%E4%BB%B6"><span class="toc-number">1.4.4.</span> <span class="toc-text">封装exe文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%9E%E6%8E%A5mongodb%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="toc-number">1.4.5.</span> <span class="toc-text">连接mongodb数据库</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/06/24/An%20Intelligent%20Mobile%20Prediction%20method/" title="An Intelligent Mobile Prediction method with Mini-batch HTIA-based Seq2Seq Networks"><img src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/model_00.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="An Intelligent Mobile Prediction method with Mini-batch HTIA-based Seq2Seq Networks"/></a><div class="content"><a class="title" href="/2023/06/24/An%20Intelligent%20Mobile%20Prediction%20method/" title="An Intelligent Mobile Prediction method with Mini-batch HTIA-based Seq2Seq Networks">An Intelligent Mobile Prediction method with Mini-batch HTIA-based Seq2Seq Networks</a><time datetime="2023-06-23T16:00:00.000Z" title="发表于 2023-06-24 00:00:00">2023-06-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/03/05/%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E4%BD%A0%E4%B8%AA%E4%BA%BA%E8%B4%A6%E5%8F%B7%E7%9A%84openai%E7%9A%84api/" title="无题"><img src="/img/rick1.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="无题"/></a><div class="content"><a class="title" href="/2023/03/05/%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E4%BD%A0%E4%B8%AA%E4%BA%BA%E8%B4%A6%E5%8F%B7%E7%9A%84openai%E7%9A%84api/" title="无题">无题</a><time datetime="2023-03-05T05:57:17.050Z" title="发表于 2023-03-05 13:57:17">2023-03-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/03/04/%E4%B8%89%E5%88%86%E9%92%9F4%E8%A1%8C%E5%91%BD%E4%BB%A4%E6%9E%84%E5%BB%BAchatgpt%20webapp,%E6%94%AF%E6%8C%81%E9%AB%98%E5%B9%B6%E5%8F%91%E4%BB%A5%E5%8F%8A%E4%B8%8A%E4%B8%8B%E6%96%87%E5%AF%B9%E8%AF%9D%E5%8A%9F%E8%83%BD/" title="三分钟4行命令构建chatgpt webapp,支持高并发以及上下文对话功能"><img src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20230305093941469.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="三分钟4行命令构建chatgpt webapp,支持高并发以及上下文对话功能"/></a><div class="content"><a class="title" href="/2023/03/04/%E4%B8%89%E5%88%86%E9%92%9F4%E8%A1%8C%E5%91%BD%E4%BB%A4%E6%9E%84%E5%BB%BAchatgpt%20webapp,%E6%94%AF%E6%8C%81%E9%AB%98%E5%B9%B6%E5%8F%91%E4%BB%A5%E5%8F%8A%E4%B8%8A%E4%B8%8B%E6%96%87%E5%AF%B9%E8%AF%9D%E5%8A%9F%E8%83%BD/" title="三分钟4行命令构建chatgpt webapp,支持高并发以及上下文对话功能">三分钟4行命令构建chatgpt webapp,支持高并发以及上下文对话功能</a><time datetime="2023-03-03T16:00:00.000Z" title="发表于 2023-03-04 00:00:00">2023-03-04</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/03/03/%E5%A4%9A%E6%A0%87%E7%AD%BE%E5%88%86%E7%B1%BB%E7%9A%84CrossEntropyLoss%E5%88%B0%E5%BA%95%E9%9C%80%E4%B8%8D%E9%9C%80%E8%A6%81One-Hot%E7%BC%96%E7%A0%81/" title="多标签分类的CrossEntropyLoss到底需不需要One-Hot编码"><img src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20230303211538791.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="多标签分类的CrossEntropyLoss到底需不需要One-Hot编码"/></a><div class="content"><a class="title" href="/2023/03/03/%E5%A4%9A%E6%A0%87%E7%AD%BE%E5%88%86%E7%B1%BB%E7%9A%84CrossEntropyLoss%E5%88%B0%E5%BA%95%E9%9C%80%E4%B8%8D%E9%9C%80%E8%A6%81One-Hot%E7%BC%96%E7%A0%81/" title="多标签分类的CrossEntropyLoss到底需不需要One-Hot编码">多标签分类的CrossEntropyLoss到底需不需要One-Hot编码</a><time datetime="2023-03-02T16:00:00.000Z" title="发表于 2023-03-03 00:00:00">2023-03-03</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/02/28/GPTEX---%E4%B8%BAChatGPT%E8%80%8C%E7%94%9F%E7%9A%84latex%E8%BD%AF%E4%BB%B6/" title="GPTEX---为ChatGPT而生的latex软件"><img src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20230228145723183.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="GPTEX---为ChatGPT而生的latex软件"/></a><div class="content"><a class="title" href="/2023/02/28/GPTEX---%E4%B8%BAChatGPT%E8%80%8C%E7%94%9F%E7%9A%84latex%E8%BD%AF%E4%BB%B6/" title="GPTEX---为ChatGPT而生的latex软件">GPTEX---为ChatGPT而生的latex软件</a><time datetime="2023-02-27T16:00:00.000Z" title="发表于 2023-02-28 00:00:00">2023-02-28</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 - 2023 By 杨一赫</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="chat_btn" type="button" title="聊天"><i class="fas fa-sms"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.1
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container').forEach(node => {
            if (node.hasAttribute('display')) {
              btf.wrap(node, 'div', { class: 'mathjax-overflow' })
            } else {
              btf.wrap(node, 'span', { class: 'mathjax-overflow' })
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax/es5/tex-mml-chtml.min.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typeset()
}</script><script>(() => {
  const $mermaidWrap = document.querySelectorAll('#article-container .mermaid-wrap')
  if ($mermaidWrap.length) {
    window.runMermaid = () => {
      window.loadMermaid = true
      const theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'default'

      Array.from($mermaidWrap).forEach((item, index) => {
        const mermaidSrc = item.firstElementChild
        const mermaidThemeConfig = '%%{init:{ \'theme\':\'' + theme + '\'}}%%\n'
        const mermaidID = 'mermaid-' + index
        const mermaidDefinition = mermaidThemeConfig + mermaidSrc.textContent
        mermaid.mermaidAPI.render(mermaidID, mermaidDefinition, (svgCode) => {
          mermaidSrc.insertAdjacentHTML('afterend', svgCode)
        })
      })
    }

    const loadMermaid = () => {
      window.loadMermaid ? runMermaid() : getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(runMermaid)
    }

    window.pjax ? loadMermaid() : document.addEventListener('DOMContentLoaded', loadMermaid)
  }
})()</script><script>function addGitalkSource () {
  const ele = document.createElement('link')
  ele.rel = 'stylesheet'
  ele.href= 'https://cdn.jsdelivr.net/npm/gitalk/dist/gitalk.min.css'
  document.getElementsByTagName('head')[0].appendChild(ele)
}

function loadGitalk () {
  function initGitalk () {
    var gitalk = new Gitalk(Object.assign({
      clientID: '5a73aec03d8a0aeb971f',
      clientSecret: '54ad1c8adab95b382d582c73e9f597781de43525',
      repo: 'comment',
      owner: 'nmhjklnm',
      admin: ['nmhjklnm'],
      id: '439722cf15b044161d708d52b1908907',
      updateCountCallback: commentCount
    },null))

    gitalk.render('gitalk-container')
  }

  if (typeof Gitalk === 'function') initGitalk()
  else {
    addGitalkSource()
    getScript('https://cdn.jsdelivr.net/npm/gitalk/dist/gitalk.min.js').then(initGitalk)
  }
}

function commentCount(n){
  let isCommentCount = document.querySelector('#post-meta .gitalk-comment-count')
  if (isCommentCount) {
    isCommentCount.innerHTML= n
  }
}

if ('Gitalk' === 'Gitalk' || !false) {
  if (false) btf.loadComment(document.getElementById('gitalk-container'), loadGitalk)
  else loadGitalk()
} else {
  function loadOtherComment () {
    loadGitalk()
  }
}</script></div><script defer="defer" id="fluttering_ribbon" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-fluttering-ribbon.min.js"></script><script src="//code.tidio.co/6zvu6bgfljrvvv9rw1y7hzehoriklr03.js" async="async"></script><script>function onTidioChatApiReady() {
  window.tidioChatApi.hide();
  window.tidioChatApi.on("close", function() {
    window.tidioChatApi.hide();
  });
}
if (window.tidioChatApi) {
  window.tidioChatApi.on("ready", onTidioChatApiReady);
} else {
  document.addEventListener("tidioChat-ready", onTidioChatApiReady);
}

var chatBtnFn = () => {
  document.getElementById("chat_btn").addEventListener("click", function(){
    window.tidioChatApi.show();
    window.tidioChatApi.open();
  });
}
chatBtnFn()
</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>